package cn.com.cbj.rabbitmq.consumer;

import cn.com.cbj.entity.KnowledgeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 确认消费模式-人为手动确认消费-监听器
 * @Author:debug (SteadyJack)
 **/
@Component("knowledgeManualConsumer")
public class KnowledgeManualConsumer implements ChannelAwareMessageListener{
    //定义日志
    private static final Logger log= LoggerFactory.getLogger(KnowledgeManualConsumer.class);
    //定义JSON序列化和反序列化组件实例
    @Autowired
    private ObjectMapper objectMapper;
    /**
     * 监听消费消息
     * @param message 消息实体
     * @param channel 通道实例
     * @throws Exception
     */
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        //获取消息属性
        MessageProperties messageProperties=message.getMessageProperties();
        //获取消息分发时的全局唯一标识
        long deliveryTag=messageProperties.getDeliveryTag();
        //捕获异常
        try {
            //获得消息体
            //byte[] msg=message.getBody();
            //解析消息体
            //KnowledgeInfo info=objectMapper.readValue(msg, KnowledgeInfo.class);
            //打印日志信息
            log.info("确认消费模式-人为手动确认消费-监听器监听消费消息-内容为：{} ",message);

            //执行完业务逻辑后，手动进行确认消费，其中第一个参数为：消息的分发标识(全局唯一);第二个参数：是否允许批量确认消费(在这里设置为true)
            channel.basicAck(deliveryTag,true);
        }catch (Exception e){
            log.info("确认消费模式-人为手动确认消费-监听器监听消费消息-发生异常：",
                    e.fillInStackTrace());

            //如果在处理消息的过程中发生了异常，则依旧需要人为手动确认消费掉该消息
            //否则该消息将一直留在队列中，从而导致消息的重复消费
            channel.basicReject(deliveryTag,false);
        }
    }
}

